package com.cpecc.dao.wuzi;

import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.WeiXiu;
import com.cpecc.model.wuzi.WuZi;
import com.cpecc.model.wuzi.YiJiao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class YiJiaoDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void save(YiJiao yiJiao){
		Session session=sessionFactory.getCurrentSession();
		session.save(yiJiao);
		session.flush();
	}
	
	public YiJiao get(String id){
		return (YiJiao) sessionFactory.getCurrentSession().get(YiJiao.class, id);
	}

	public YiJiao getByLiuChengId(String liuChengId){
		String hql="from YiJiao t where t.liuChengId = ?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, liuChengId);
		List<YiJiao> list = query.list();
		return CollectionUtils.isEmpty(list) ? null : list.get(0);
	}
	
	public boolean cover(YiJiao yiJiao){
		String hql="update YiJiao t set t.huanJie=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, yiJiao.getHuanJie());
		query.setString(1, yiJiao.getId());
		return query.executeUpdate()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<YiJiao> getMyTuiHui(User user){
		String hql="from YiJiao t where t.liuChengLX=1 and t.yiJiaoR.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<YiJiao> getMyYiJiao(User user){
		String hql="from YiJiao t where t.liuChengLX=2 and t.yiJiaoR.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<YiJiao> getAllTuiHui(){
		String hql="from YiJiao t where t.liuChengLX=1 and t.huanJie.id=602 order by t.shenQingRQ desc";
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}

	public List<YiJiao> getAllYiJiao(){
		String hql="from YiJiao t order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<YiJiao> getAllYiJiao(int huanJie){
		String hql="from YiJiao t where t.liuChengLX=2 and t.huanJie.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, huanJie);
		return query.list();
	}

	public YiJiao getYiJiaoByWuZi(WuZi wuZi){
		String hql="from YiJiao t where t.wuZi=? and t.liuChengLX=2";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, wuZi);
		if(query.list().size()>0)
			return (YiJiao)query.list().get(0);
		else
			return null;
	}
	
	public YiJiao getYiJiaoByWuZi(WuZi wuZi,int huanJie){
		String hql="from YiJiao t where t.wuZi=? and t.liuChengLX=2 and t.huanJie.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, wuZi);
		query.setInteger(1, huanJie);
		if(query.list().size()>0)
			return (YiJiao)query.list().get(0);
		else
			return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<YiJiao> getMyYiJiaoJS(User user){
		String hql="from YiJiao t where t.liuChengLX=2 and t.yiJiaoR.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<YiJiao> getAllYiJiaoByWuZi(WuZi wuZi){		
		String hql="from YiJiao t where t.liuChengLX=2 and t.wuZi=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, wuZi);		
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<YiJiao> getAllTuiHuanByWuZi(WuZi wuZi){		
		String hql="from YiJiao t where t.liuChengLX=1 and t.wuZi=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);		
		query.setParameter(0, wuZi);
		return query.list();
	}
	
	public boolean hasWuZi(String wuZiId){
		String hql="from YiJiao t where t.wuZi.id=? and (t.huanJie.id<506 or (t.huanJie.id>600 and t.huanJie.id<603))";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, wuZiId);
		return query.list().size()>0;
	}
	
	public boolean hasLiuCheng(User user){
		String hql="from YiJiao t where t.yiJiaoR=? and (t.huanJie.id<506 or (t.huanJie.id>600 and t.huanJie.id<603))";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, user);
		return query.list().size()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<YiJiao> activeYiJiao(){
		String hql="from YiJiao t where t.liuChengLX=2 and t.huanJie.id<506 order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}

	public boolean delete(String id) {
		String hql="delete YiJiao t where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);
		return query.executeUpdate()>0;
	}
}
